﻿Imports System.Web.Configuration

Partial Class WebForm_MasterPage
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.title1.Text += " " + WebConfigurationManager.AppSettings("Version")
        Me.lblUserName.Text = "登入者：" & clsAuth.GetUserID & "(" & HttpUtility.UrlDecode(Request.Cookies("UserName").Value) & ")"
	Me.lblOrganization.Text = "所屬單位：" & clsAuth.GetOrganizationName(clsAuth.GetUserID)

        If Not IsPostBack Then

            If Me.CheckAuth() = True Then
                Me.FixAjaxBug()
                Me.BindData()
                'Me.BindPublicMenu()
                Me.BindMenu()
                Me.BindBUContactInfo()
                Me.BindSystemContactInfo()
            Else
                MyObj.ShowMessage(Me.GetLocalResourceObject("CheckAuth_Failure"))
            End If

        End If
    End Sub

    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogout.Click
        'Response.Redirect("~\Default.aspx")
        Response.Redirect("~\WebForm\Logout.aspx")
    End Sub

    Protected Sub btnHome_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnHome.Click
        'Response.Redirect("~\WebForm\WebForm1.aspx")
        Response.Redirect(clsAuth.GetDefaultUrl())
    End Sub

    Protected Sub Menu1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Menu1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim dt As Data.DataTable
            Dim dat As New clsData
            Dim sql As New StringBuilder

            sql.AppendFormat("select distinct p.* ")
            sql.AppendFormat("from FD_ProtectedProgram p ")
            sql.AppendFormat("inner join FD_RoleProgramMappingView rp on p.ProgramID = rp.ProgramID ")
            sql.AppendFormat("where p.Parent = '{0}' ", TryCast(e.Item.FindControl("MenuItem"), WebForm_CommonControl_MySlidingMenuGroup).ProgramID)
            sql.AppendFormat("and p.Visible = 'Y' ")
            sql.AppendFormat("and rp.RoleID in ({0})", clsAuth.GetRoleList())
            sql.AppendFormat("union select distinct p.* ")
            sql.AppendFormat("from FD_PublicProgram p ")
            sql.AppendFormat("where p.Parent = '{0}' ", TryCast(e.Item.FindControl("MenuItem"), WebForm_CommonControl_MySlidingMenuGroup).ProgramID)
            sql.AppendFormat("and p.Visible = 'Y' and isnull(p.TableName,'')<>'OnlyExternal' ") 'OnlyExternal只在未登入時才會看見
            sql.AppendFormat("order by p.Sequence ")

            dt = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)

            TryCast(e.Item.FindControl("MenuItem"), WebForm_CommonControl_MySlidingMenuGroup).BindGV(dt)

            If DataBinder.Eval(e.Item.DataItem, "ProgramType") = "Public" Then
                TryCast(e.Item.FindControl("MenuItem"), WebForm_CommonControl_MySlidingMenuGroup).ProgramDirectory = "Public"
            End If
        End If
    End Sub

End Class

Partial Class WebForm_MasterPage
    Private Sub BindBUContactInfo()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select ParameterValue as BUContactInfo from FD_SystemParameter ")
        sql.AppendFormat("where ParameterType = 'Home' and ParameterName = 'BUContactInfo' and Active='Y' ")

        Me.fvBUContactInfo.ChangeMode(FormViewMode.ReadOnly)
        Me.fvBUContactInfo.DataSource = dat.GetData(sql)
        Me.fvBUContactInfo.DataBind()

    End Sub

    Private Sub BindSystemContactInfo()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select ParameterValue as SystemContactInfo from FD_SystemParameter ")
        sql.AppendFormat("where ParameterType = 'Home' and ParameterName = 'SystemContactInfo' and Active='Y' ")

        Me.fvSystemContactInfo.ChangeMode(FormViewMode.ReadOnly)
        Me.fvSystemContactInfo.DataSource = dat.GetData(sql)
        Me.fvSystemContactInfo.DataBind()

    End Sub

    Private Function CheckAuth() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New Data.DataTable

        '檢查此表單是否需控管
        sql.Length = 0
        sql.AppendFormat("select p.* from FD_ProtectedProgram p ")
        sql.AppendFormat("where p.Url = '{0}' ", clsAuth.GetUserID())

        dt = dat.GetData(sql)

        If dt.Rows.Count = 0 Then
            '不需控管，直接跳過檢查
            Return True
        End If

        sql.Length = 0
        sql.AppendFormat("select pa.* from FD_UserProgramAuth pa ")
        sql.AppendFormat("inner join FD_ProtectedProgram p on pa.ProgramID = p.ProgramID ")
        sql.AppendFormat("where p.Active = 'Y' ")
        sql.AppendFormat("and p.Url = '{0}' ", clsAuth.GetProgramUrl())
        sql.AppendFormat("and pa.UserID = '{0}' ", clsAuth.GetUserID())

        dt = dat.GetData(sql)

        If dt.Rows.Count = 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    Private Sub FixAjaxBug()
        'CalendarExtendar with MultiView + UpdatePanel bug
        'http://dotnetrush.blogspot.com/2008/02/ajax-calendarextender-bug-fixes.html

        Dim url As String = Page.ClientScript.GetWebResourceUrl(GetType(AjaxControlToolkit.ClientCssResourceAttribute), "AjaxControlToolkit.Calendar.Calendar.css")
        Dim myHtmlLink As New HtmlLink()
        myHtmlLink.Href = url
        myHtmlLink.Attributes.Add("rel", "stylesheet")
        myHtmlLink.Attributes.Add("type", "text/css")
        Page.Header.Controls.Add(myHtmlLink)
    End Sub

    Private Sub BindSiteMap()
        'Me.SiteMapPath1.currentno()
    End Sub

    Private Sub BindData()
        'Me.btnLogout.Text = String.Format("{0} {1}", Request.Cookies("UserName").Value, Me.GetLocalResourceObject("btnLogout.Text"))
    End Sub

    Private Sub BindMenu()
        Dim dt As Data.DataTable
        Dim dr As Data.DataRow

        dt = New Data.DataTable
        dt.Columns.Add("MenuName")
        dt.Columns.Add("Url")

        dr = dt.NewRow
        dr("MenuName") = "表單權限管理"
        dr("Url") = "ManageOrganization.aspx"
        dt.Rows.Add(dr)

        Dim dat As New clsData
        Dim sql As New StringBuilder

        sql.AppendFormat(" select distinct p.* ")
        sql.AppendFormat("from FD_PublicProgram p ")
        sql.AppendFormat("where p.Parent = 0 ")
        sql.AppendFormat("union select distinct p.* ")
        sql.AppendFormat("from FD_ProtectedProgram p ")
        sql.AppendFormat("inner join FD_RoleProgramMappingView rp on p.ProgramID = rp.ProgramID ")
        sql.AppendFormat("where p.Parent = 0 ")
        sql.AppendFormat("and rp.RoleID in ({0})", clsAuth.GetRoleList())


        sql.AppendFormat("order by p.Sequence ")

        Me.Menu1.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.Menu1.DataBind()

    End Sub

    Public ReadOnly Property SiteMapPath() As SiteMapPath
        Get
            Return Me.SiteMapPath1
        End Get
    End Property
End Class
